// 支付记录实体（Payment）
import { Entity, Column, Index, ManyToOne, JoinColumn } from 'typeorm';
import { BaseEntity } from '../common/base/base.entity';
import { User } from './user.entity';
import { Order } from './order.entity';

@Entity('payments')
@Index('idx_payment_no', ['paymentNo'], { unique: true })
export class Payment extends BaseEntity {
  @Column({
    type: 'bigint',
    comment: '订单ID',
  })
  orderId: string;

  @Column({
    type: 'bigint',
    comment: '用户ID',
  })
  userId: string;

  @Column({
    type: 'varchar',
    length: 64,
    unique: true,
    comment: '支付流水号',
  })
  paymentNo: string;

  @Column({
    type: 'varchar',
    length: 64,
    nullable: true,
    comment: '第三方交易号',
  })
  tradeNo?: string;

  @Column({
    type: 'enum',
    enum: ['alipay', 'wechat'],
    comment: '支付方式',
  })
  paymentMethod: 'alipay' | 'wechat';

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    comment: '支付金额',
  })
  amount: number;

  @Column({
    type: 'enum',
    enum: ['pending', 'success', 'failed', 'refunded'],
    default: 'pending',
    comment: '支付状态',
  })
  status: 'pending' | 'success' | 'failed' | 'refunded';

  @Column({
    type: 'json',
    nullable: true,
    comment: '回调数据',
  })
  notifyData?: any;

  @Column({
    type: 'timestamp',
    nullable: true,
    comment: '支付成功时间',
  })
  paidAt?: Date;

  @Column({
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: true,
    comment: '退款金额',
  })
  refundAmount?: number;

  @Column({
    type: 'varchar',
    length: 500,
    nullable: true,
    comment: '退款原因',
  })
  refundReason?: string;

  @Column({
    type: 'timestamp',
    nullable: true,
    comment: '退款时间',
  })
  refundedAt?: Date;

  // 关联
  @ManyToOne(() => User)
  @JoinColumn({ name: 'userId' })
  user: User;

  @ManyToOne(() => Order)
  @JoinColumn({ name: 'orderId' })
  order: Order;
}
